package com.wx.mall.goods.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wx.mall.common.utils.DataResult;
import com.wx.mall.common.vo.goods.BrandBaseVo;
import com.wx.mall.goods.entity.Brand;
import com.wx.mall.goods.service.IBrandService;
import com.wx.mall.goods.vo.req.BrandBatchReqVO;
import com.wx.mall.goods.vo.req.BrandPageReqVO;
import com.wx.mall.goods.vo.req.BrandReqVO;
import com.wx.mall.goods.vo.req.PageReqVO;
import com.wx.mall.goods.vo.resp.BrandRespVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 品牌前端控制器
 * </p>
 *
 * @author ljl
 * @since 2020-03-17
 */
@RestController
@RequestMapping("/proBrand")
@Api(tags = "品牌管理相关接口")
public class BrandController {

    @Autowired
    private IBrandService brandService;

    @GetMapping("/brands/baseAvailable")
    @ApiOperation("分页获取品牌数据")
    public DataResult list() {
        List<BrandBaseVo> list = brandService.listBaseAvailable();
        return DataResult.success(list);
    }

    @GetMapping("/brands")
    @ApiOperation("分页获取品牌数据")
    public DataResult pageList(@ApiParam("分页vo") @Valid BrandPageReqVO vo) {
        Page<Brand> brandPage = brandService.pageList(vo);
        return DataResult.success(brandPage);
    }

    @GetMapping("/brands/available")
    @ApiOperation("分页获取品牌数据(不包含已删)")
    public DataResult pageInfo(@ApiParam("分页vo") @Valid BrandPageReqVO vo) {
        Page<Brand> brandPage = brandService.pageInfo(vo);
        return DataResult.success(brandPage);
    }

    @GetMapping("/brands/available/all")
    @ApiOperation("获取品牌数据(不包含已删)")
    public DataResult availableInfos() {
        List<BrandRespVO> infos = brandService.availableInfos();
        return DataResult.success(infos);
    }

    @GetMapping("/brands/match")
    @ApiOperation("根据品牌名称获取品牌数据")
    public DataResult pageMatch(@ApiParam("分页vo") @Valid BrandPageReqVO vo) {
        Page<Brand> brandPage = brandService.pageMatch(vo);
        return DataResult.success(brandPage);
    }

    @PutMapping("/brands/generateInitial")
    @ApiOperation("当前页生成首字母，并获取品牌数据")
    public DataResult initialGenerator(@ApiParam("分页vo") @Valid PageReqVO vo) {
        Page<Brand> brandPage = brandService.initialGenerator(vo);
        return DataResult.success(brandPage);
    }

    @GetMapping("/brand/{id}")
    @ApiOperation("根据id查询品牌数据")
    public DataResult findById(@ApiParam("品牌id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        Brand brand = brandService.findById(id);
        return DataResult.success(brand);
    }

    @DeleteMapping("/brand/{id}")
    @ApiOperation("根据id删除品牌数据")
    public DataResult softDeleteById(@ApiParam("品牌id") @PathVariable("id") @NotNull(message = "id不能为空") Integer id) {
        brandService.softDeleteById(id);
        return DataResult.success();
    }
    @PostMapping("/brands")
    @ApiOperation("根据ids批量操作品牌数据")
    public DataResult batchOperation(@ApiParam("批量处理请求vo") @Valid BrandBatchReqVO vo) {
        brandService.batchOperation(vo);
        return DataResult.success();
    }

    @PutMapping("/brand")
    @ApiOperation("根据id更新品牌数据")
    public DataResult updateById(@Valid BrandReqVO reqVO) {
        brandService.updateById(reqVO);
        return DataResult.success();
    }

    @PostMapping("/brand")
    @ApiOperation("新增品牌信息")
    public DataResult add(@Valid BrandReqVO reqVO) {
        brandService.add(reqVO);
        return DataResult.success();
    }

    // ElementUI el-upload 带参数上传文件 后台接受方式
    @PostMapping("/uploadImage")
    @ApiOperation("品牌logo图标上传")
    public DataResult uploadImage(@ApiParam("image文件") @RequestParam("file") MultipartFile file, @ApiParam("brand对象名称") @RequestParam Map<String,String> param) {
        //获取http服务访问端口
//        String logoUrlPrefix = req.getRequestURL().toString().replace(req.getRequestURI(), "");

        String name = param.get("name").trim();
        Resource resource = file.getResource();
        Map<String, String> map = brandService.uploadLogo(file, name);
        return DataResult.success(map);
    }

}

